package lc.q151_200;

import java.util.Arrays;

public class Q186 {

    public void reverseWords(char[] s) {
        // 原地解法求解
        // 从头到尾颠倒
        for (int i = 0; i < s.length / 2; i++) {
            char temp = s[i];
            s[i] = s[s.length - i - 1];
            s[s.length - i - 1] = temp;
        }
        System.out.println(new String(s));
        // 两个指针控制空格
        int i = 0;
        int j = 0;
        while (i < s.length) {
            while (j < s.length && s[j] !=' ') {
                j++;
            }
            // 颠倒
            for (int k = 0; k < (j - i) / 2; k++) {
                char temp = s[i + k];
                s[i + k] = s[j - k - 1];
                s[j-k-1] = temp;
            }
            i = j+1;
            j = j +1;
        }
    }

    public static void main(String[] args) {
        String t = "the sky is blue";
        char[] s = Arrays.copyOf(t.toCharArray(), t.length());
        Q186 q186 = new Q186();
        q186.reverseWords(s);
        System.out.println(new String(s));
    }

}
